package com.compass.doctor.ui.chat.util


import android.content.Context
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.RequiresApi
import com.compass.doctor.R
import com.compass.doctor.callback.OnChatMenuClick
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.preview.PreviewPicActivity
import com.compass.doctor.ui.WebDetailActivity
import com.compass.doctor.ui.chat.attachment.ArticleAttachment
import com.compass.doctor.ui.chat.attachment.ExamineAttachment
import com.compass.doctor.ui.chat.attachment.GiftMessageAttachment
import com.compass.doctor.ui.chat.attachment.HospitalizationCertificateAttachment
import com.compass.doctor.ui.chat.attachment.InquiryAttachment
import com.compass.doctor.ui.chat.attachment.PostDiagnosisAttachment
import com.compass.doctor.ui.chat.attachment.PreConsultationAttachment
import com.compass.doctor.ui.chat.attachment.PrescriptionAttachment
import com.compass.doctor.ui.chat.view.CustomChatBottomView
import com.compass.doctor.ui.chat.view.CustomChatTopView
import com.compass.doctor.ui.chat.viewholder.ArticleViewHolder
import com.compass.doctor.ui.chat.viewholder.ExamineViewHolder
import com.compass.doctor.ui.chat.viewholder.GiftMessageViewHolder
import com.compass.doctor.ui.chat.viewholder.HospitalizationCertificateViewHolder
import com.compass.doctor.ui.chat.viewholder.InquiryViewHolder
import com.compass.doctor.ui.chat.viewholder.PostDiagnosisViewHolder
import com.compass.doctor.ui.chat.viewholder.PreConsultationViewHolder
import com.compass.doctor.ui.chat.viewholder.PrescriptionViewHolder
import com.compass.doctor.ui.home.DoctorChatActivity
import com.compass.doctor.ui.mine.DoctorHomepageActivity
import com.compass.doctor.ui.mine.HospitalizedDetailsActivity
import com.compass.doctor.ui.patient.PatientChatActivity
import com.compass.doctor.ui.patient.PractitionerToPatientChatActivity
import com.compass.framework.constant.ARTICLE_CODE
import com.compass.framework.constant.ARTICLE_DETAILS
import com.compass.framework.constant.CERTIFICATE_CODE
import com.compass.framework.constant.EXAMINE_CODE
import com.compass.framework.constant.GIVE_CODE
import com.compass.framework.constant.INQUIRY_CODE
import com.compass.framework.constant.NIM_APP_KEY
import com.compass.framework.constant.POST_DIAGNOSIS_CODE
import com.compass.framework.constant.PRESCRIPTION_CODE
import com.compass.framework.constant.RX_DETAILS
import com.compass.framework.constant.SUBSCRIBE_CODE
import com.compass.framework.constant.VIDEO_DETAILS
import com.compass.framework.ext.gone
import com.compass.framework.ext.toJson
import com.compass.framework.helper.CompassAppHelper
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.model.UnderwayOrder
import com.compass.framework.utils.DateUtils
import com.compass.framework.utils.LogUtil
import com.compass.framework.utils.dpToPx
import com.compass.framework.utils.getColorDrawable
import com.compass.framework.utils.getColorFromResource
import com.compass.framework.utils.getStringFromResource
import com.luck.picture.lib.entity.LocalMedia
import com.netease.lava.nertc.sdk.NERtcOption
import com.netease.nimlib.sdk.Observer
import com.netease.nimlib.sdk.StatusCode
import com.netease.nimlib.sdk.avsignalling.constant.ChannelType
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum
import com.netease.yunxin.kit.call.p2p.model.NECallInitRtcMode
import com.netease.yunxin.kit.call.p2p.model.NEInviteInfo
import com.netease.yunxin.kit.chatkit.repo.ContactRepo.getUserInfoFromLocal
import com.netease.yunxin.kit.chatkit.ui.ChatKitClient
import com.netease.yunxin.kit.chatkit.ui.ChatUIConfig
import com.netease.yunxin.kit.chatkit.ui.IChatInputMenu
import com.netease.yunxin.kit.chatkit.ui.builder.IChatViewCustom
import com.netease.yunxin.kit.chatkit.ui.`fun`.view.FunChatView
import com.netease.yunxin.kit.chatkit.ui.`fun`.view.MessageBottomLayout
import com.netease.yunxin.kit.chatkit.ui.interfaces.IChatView
import com.netease.yunxin.kit.chatkit.ui.interfaces.IMessageItemClickListener
import com.netease.yunxin.kit.chatkit.ui.model.ChatMessageBean
import com.netease.yunxin.kit.chatkit.ui.view.input.ActionConstants
import com.netease.yunxin.kit.chatkit.ui.view.message.MessageProperties
import com.netease.yunxin.kit.chatkit.ui.view.popmenu.ChatPopMenuAction
import com.netease.yunxin.kit.chatkit.ui.view.popmenu.IChatPopMenu
import com.netease.yunxin.kit.chatkit.ui.view.useful.IExpressionButtonClickListener
import com.netease.yunxin.kit.common.ui.action.ActionItem
import com.netease.yunxin.kit.conversationkit.ui.ConversationKitClient
import com.netease.yunxin.kit.conversationkit.ui.ConversationUIConfig
import com.netease.yunxin.kit.conversationkit.ui.ItemClickListener
import com.netease.yunxin.kit.conversationkit.ui.common.ConversationConstant
import com.netease.yunxin.kit.conversationkit.ui.model.ConversationBean
import com.netease.yunxin.kit.corekit.im.IMKitClient.getAuthServiceObserver
import com.netease.yunxin.kit.corekit.im.custom.CustomAttachment
import com.netease.yunxin.kit.corekit.im.model.UserInfo
import com.netease.yunxin.kit.meeting.sdk.NECallback
import com.netease.yunxin.kit.meeting.sdk.NEMeetingKit
import com.netease.yunxin.kit.meeting.sdk.NEMeetingKitConfig
import com.netease.yunxin.kit.meeting.sdk.config.NEForegroundServiceConfig
import com.netease.yunxin.nertc.ui.CallKitNotificationConfig
import com.netease.yunxin.nertc.ui.CallKitUI.destroy
import com.netease.yunxin.nertc.ui.CallKitUI.init
import com.netease.yunxin.nertc.ui.CallKitUIOptions


/**
 * Description： 自定义云信UI 配置类
 * Date:2023/11/29 16:45
 */

object CustomUIConfig {

    private var chatUIConfig = ChatUIConfig()//会话UIConfig
    private var conversationUIConfig = ConversationUIConfig()//会话列表Config
    private var messageProperties = MessageProperties();//消息页面的UI个性化定制，包括常用的字体颜色、大小、是否可见、背景色等属性等

    private var chatTop: CustomChatTopView? = null //会话顶部自定义view
    private var bottomLayout: MessageBottomLayout? = null //会话底部自定义view


    /**
     * 添加自定义消息attach
     */
    fun initChatCustomAttach() {
        ChatKitClient.addCustomAttach(INQUIRY_CODE, InquiryAttachment::class.java)
        ChatKitClient.addCustomAttach(POST_DIAGNOSIS_CODE, PostDiagnosisAttachment::class.java)
        ChatKitClient.addCustomAttach(PRESCRIPTION_CODE, PrescriptionAttachment::class.java)
        ChatKitClient.addCustomAttach(
            CERTIFICATE_CODE,
            HospitalizationCertificateAttachment::class.java
        )
        ChatKitClient.addCustomAttach(EXAMINE_CODE, ExamineAttachment::class.java)
        ChatKitClient.addCustomAttach(GIVE_CODE, GiftMessageAttachment::class.java)
        ChatKitClient.addCustomAttach(ARTICLE_CODE, ArticleAttachment::class.java)
        ChatKitClient.addCustomAttach(SUBSCRIBE_CODE, PreConsultationAttachment::class.java)
    }

    /**
     * 添加自定义viewHolder
     */
    fun initChatCustomViewHolder() {
        ChatKitClient.addCustomViewHolder(INQUIRY_CODE, InquiryViewHolder::class.java)
        ChatKitClient.addCustomViewHolder(POST_DIAGNOSIS_CODE, PostDiagnosisViewHolder::class.java)
        ChatKitClient.addCustomViewHolder(PRESCRIPTION_CODE, PrescriptionViewHolder::class.java)
        ChatKitClient.addCustomViewHolder(
            CERTIFICATE_CODE,
            HospitalizationCertificateViewHolder::class.java
        )
        ChatKitClient.addCustomViewHolder(EXAMINE_CODE, ExamineViewHolder::class.java)
        ChatKitClient.addCustomViewHolder(ARTICLE_CODE, ArticleViewHolder::class.java)
        ChatKitClient.addCustomViewHolder(GIVE_CODE, GiftMessageViewHolder::class.java)
        ChatKitClient.addCustomViewHolder(SUBSCRIBE_CODE, PreConsultationViewHolder::class.java)
    }


    /**
     * 个性化配置会话消息页面
     */
    fun configChatKit(context: Context) {

        //头像的圆角，0 代表方形，30dp 为原型。这里需要自行将 dp 转换为 px
        messageProperties.avatarCornerRadius = dpToPx(30f)
        //文本消息字体颜色
        messageProperties.messageTextColor = getColorFromResource(R.color.color_2e344a)
        messageProperties.messageTextOutColor = getColorFromResource(R.color.white)
        //文本消息字体大小
        messageProperties.messageTextSize = 16
        //设置时间字体大小
        messageProperties.timeTextSize = 14
        //时间字体颜色
        messageProperties.timeTextColor = getColorFromResource(R.color.color_999999)
        messageProperties.chatViewBackground = getColorDrawable(R.color.color_f2f2f2)
        messageProperties.showTitleBar = false//不显示会话标题栏
        chatUIConfig.messageProperties = messageProperties

        chatUIConfig.chatPopMenu = object : IChatPopMenu {

            override fun showDefaultPopMenu(): Boolean {
                return true
            }

            @RequiresApi(Build.VERSION_CODES.N)
            override fun customizePopMenu(menuList: MutableList<ChatPopMenuAction>?, messageBean: ChatMessageBean?): MutableList<ChatPopMenuAction> {

                menuList?.let {
                    menuList.removeIf {
                        it.action.equals(ActionConstants.POP_ACTION_PIN)//移除标记按钮
                    }
                    menuList.removeIf {
                        it.action.equals(ActionConstants.POP_ACTION_TRANSMIT)//移除转发按钮
                    }
                    menuList.removeIf {
                        it.action.equals(ActionConstants.POP_ACTION_DELETE)//移除转发按钮
                    }

                }

                return super.customizePopMenu(menuList, messageBean)
            }
        }
        //1、实现点击事件

        chatUIConfig.messageItemClickListener = object : IMessageItemClickListener {
            override fun onMessageClick(
                view: View?, position: Int, messageInfo: ChatMessageBean?
            ): Boolean {
                messageInfo?.let {
                    if (it.messageData.message.msgType == MsgTypeEnum.custom) {
                        val customAttachment = it.messageData.message.attachment as CustomAttachment
                        LogUtil.e("customAttachment==${customAttachment.toJson()}")
                        when (customAttachment.type) {
                            PRESCRIPTION_CODE -> {
                                val attachment =
                                    it.messageData.message.attachment as PrescriptionAttachment
                                WebDetailActivity.start(
                                    context = context,
                                    url = RX_DETAILS + attachment.getRxId(),
                                    title = "处方详情"
                                )
                            }

                            ARTICLE_CODE -> {
                                val attachment =
                                    it.messageData.message.attachment as ArticleAttachment
                                when (attachment.getColumnType()) {
                                    "1" -> {
                                        WebDetailActivity.start(
                                            context = context,
                                            url = ARTICLE_DETAILS + attachment.getId(),
                                            title = attachment.getTitle()
                                        )
                                    }

                                    "2" -> {
                                        WebDetailActivity.start(
                                            context = context,
                                            url = VIDEO_DETAILS + attachment.getId(),
                                            title = attachment.getTitle()
                                        )
                                    }
                                }


                            }

                            CERTIFICATE_CODE -> {
                                val attachment =
                                    it.messageData.message.attachment as HospitalizationCertificateAttachment
                                HospitalizedDetailsActivity.start(context, attachment.getId())

                            }
                        }

                    }
                }

                return super.onMessageClick(view, position, messageInfo)
            }


            override fun onSelfIconClick(
                view: View?,
                position: Int,
                messageInfo: ChatMessageBean?
            ): Boolean {
                //跳转到个人信息页面
                DoctorHomepageActivity.start(context)
                return false


            }
        }


        ChatKitClient.setChatUIConfig(chatUIConfig)

    }

    /**
     * 个性化配置会话列表页面
     */
    fun configConversation() {
        //头像的圆角，0 代表方形，30dp 为原型。这里需要自行将 dp 转换为 px
        conversationUIConfig.avatarCornerRadius = dpToPx(30f)
        conversationUIConfig.itemClickListener = object : ItemClickListener {
            override fun onClick(context: Context, data: ConversationBean, position: Int): Boolean {
                //可以参考默认实现，默认实现是跳转到聊天页面
                if (data.viewType == ConversationConstant.ViewType.CHAT_VIEW) {
                    //如果是单聊
                    var userInfo = data.infoData.userInfo as UserInfo
//

                    userInfo.extensionMap?.let {
                        val userType = it["userType"]
                        val userId = it["userId"]
                        when (userType) {
                            4 -> {
                                if (userId == null) {
                                    DoctorChatActivity.start(context, userInfo)
                                } else {


                                    if (UserInfoManager.getUserType()=="1"){
                                        PatientChatActivity.start(
                                            context,
                                            userInfo,
                                            userId.toString()
                                        )
                                    }else{
                                        PractitionerToPatientChatActivity.start(
                                            context,
                                            userInfo,
                                            userId.toString()
                                        )
                                    }
                                }
                            }

                            else -> {
                                DoctorChatActivity.start(context, userInfo)
                            }
                        }
                    } ?: kotlin.run {
                        DoctorChatActivity.start(context, userInfo)
                    }

                }
                return true
            }

            override fun onLongClick(
                context: Context,
                data: ConversationBean,
                position: Int
            ): Boolean {
                //添加您的代码逻辑，如果需要继续执行默认逻辑则返回false,否则返回true
                return false
            }
        }
        ConversationKitClient.setConversationUIConfig(conversationUIConfig)
    }


    /**
     * 配置输入框上方和chat顶部增加布局
     * @param isPatient 是否是患者  患者才添加自定义布局
     * @param isInquiry 是否有正在进行中的问诊  有进行中的才添加快速开药
     */
    fun initChatBottomAndTopLayout(
        isPatient: Boolean,
        isInquiry: Boolean,
        onChatBottomClick: OnCustomButtonClick?,
        onIExpressionButtonClickListener: IExpressionButtonClickListener?,
        underwayOrder: UnderwayOrder? = null
    ) {

        chatUIConfig.chatViewCustom = IChatViewCustom { layout: IChatView ->
            if (layout is FunChatView) {
                val layoutParams = FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
                )
                // 从ChatView中获取底部消息类型布局自定义
                val frameLayoutBottom: FrameLayout = layout.chatBodyBottomLayout
                //效果截图中红色框中的布局视图View
                val customChatBottom =
                    CustomChatBottomView(CompassAppHelper.getApplication(), null, onChatBottomClick)
                //是否显示快速开药
                customChatBottom.showPrescribe(isInquiry,underwayOrder?.ifReturnVisit)
//                customChatBottom.showPrescribe(false,underwayOrder?.ifReturnVisit)
                //是否显示赠送按钮
                customChatBottom.showGift()
                //把自定义View添加到聊天页面中
                frameLayoutBottom.addView(customChatBottom, layoutParams)
                // 从ChatView中获取顶部消息类型布局自定义
                val frameLayoutTop = layout.chatBodyTopLayout
                //效果截图中红色框中的布局视图View
                chatTop = CustomChatTopView(CompassAppHelper.getApplication(), null)
                //把自定义View添加到聊天页面中
                frameLayoutTop.addView(chatTop, layoutParams)
                underwayOrder?.let {
                    setTopTips(it)
                }

                //如果是医生就隐藏自定义UI
                if (!isPatient) {
                    frameLayoutBottom.gone()
                    frameLayoutTop.gone()
                }

                //获取底部自定义常用语view
                bottomLayout = layout.inputView
                bottomLayout?.setExpressionButtonClickListener(onIExpressionButtonClickListener)

            }
        }
        ChatKitClient.setChatUIConfig(chatUIConfig)
    }


    fun setTopTips(underwayOrder: UnderwayOrder) {
        if (underwayOrder.consultResidueNum > 0) {
            chatTop?.setTipsInfo(
                "当前仅剩余条数:${underwayOrder.consultResidueNum} 条，将在${
                    DateUtils.conversionTime(
                        underwayOrder.consultResidueEndTime,
                        DateUtils.FORMAT_TWO
                    )
                }到期", "1"
            )
        } else {
//            chatTop?.setTipsInfo(null,"1")
            chatTop?.setTipsInfo(null, "1")
        }
    }

    fun setReEditText(content:String?){
        bottomLayout?.setReEditMessage(content)
    }

    fun showUseful() {
        bottomLayout?.switchUseful()
    }


    /**
     * 配置more菜单按钮
     * @param isDoctorIM 是否是医生聊天
     * @param isInquiry 是否有正在进行中的问诊
     * @param ifReturnVisit 是否是复诊 1是复诊
     */
    fun initChatMoreMenu(
        isDoctorIM: Boolean,
        isInquiry: Boolean,
        ifReturnVisit: String?,
        onChatMenuClick: OnChatMenuClick?
    ) {
        //配置输入框下面输入按钮
        chatUIConfig.chatInputMenu = object : IChatInputMenu {
            override fun customizeInputBar(actionItemList: List<ActionItem>): List<ActionItem> {
                return actionItemList
            }

            @RequiresApi(Build.VERSION_CODES.N)
            override fun customizeInputMore(actionItemList: MutableList<ActionItem>): List<ActionItem> {
                for (actionItem in actionItemList) {

                    when (actionItem.action) {
                        //修改系统自带item图标
                        ActionConstants.ACTION_TYPE_CAMERA -> {
                            actionItem.iconResId = R.mipmap.ic_chat_shoot
                        }

                        ActionConstants.ACTION_TYPE_ALBUM -> {
                            actionItem.iconResId = R.mipmap.ic_chat_picture
                        }
                    }
                }

                //是否验证复诊
//                val isFollowUp = UserInfoManager.getUserInfo()?.appHospitalConfigRespVO?.isFollowUp
                val isFollowUp = "100"//写死为不验证复诊


                if (!isDoctorIM){
                    //不是医生IM移除音视频
                    actionItemList.removeIf {
                        it.action.equals(ActionConstants.ACTION_TYPE_VIDEO_CALL)//移除音视频通话按钮
                    }
                    //添加科普分享
                    actionItemList.add(
                        4,
                        ActionItem(
                            ActionConstants.CUSTOM_ACTION_TYPE_COUPE,
                            R.mipmap.ic_chat_coupe,
                            R.string.input_menu_coupe
                        )
                    )

                }
                //不是医生对医生IM 且有正在进行中的问诊单 且用户是医生
                if (!isDoctorIM&&isInquiry&&UserInfoManager.getUserType()=="1") {
                    //添加填写病历
                    actionItemList.add(
                        1,
                        ActionItem(
                            ActionConstants.CHAT_CASE_HISTORY,
                            R.mipmap.chat_case_history,
                            R.string.chat_case_history
                        )
                    )

                    //验证是否是复诊 且是复诊
                    if (isFollowUp=="1"&&ifReturnVisit=="1"){
                        actionItemList.add(
                            0,
                            ActionItem(
                                ActionConstants.CUSTOM_ACTION_TYPE_PRESCRIBING,
                                R.mipmap.ic_chat_prescribing,
                                R.string.input_menu_prescribing
                            )
                        )


//                        actionItemList.add(2,
//                            ActionItem(
//                                ActionConstants.CHAT_HOSPITALIZATION_CERTIFICATE,
//                                R.mipmap.icon_hospitalization_certificate,
//                                R.string.chat_hospitalization_certificate
//                            )
//                        )
//
//                        actionItemList.add(
//                            3,
//                            ActionItem(
//                                ActionConstants.CUSTOM_ACTION_TYPE_INSPECT,
//                                R.mipmap.ic_chat_inspect,
//                                R.string.input_menu_inspect
//                            )
//                        )
                    }

                    //不验证复诊
                    if (isFollowUp!="1") {
                        actionItemList.add(
                            0,
                            ActionItem(
                                ActionConstants.CUSTOM_ACTION_TYPE_PRESCRIBING,
                                R.mipmap.ic_chat_prescribing,
                                R.string.input_menu_prescribing
                            )
                        )

//                        actionItemList.add(2,
//                            ActionItem(
//                                ActionConstants.CHAT_HOSPITALIZATION_CERTIFICATE,
//                                R.mipmap.icon_hospitalization_certificate,
//                                R.string.chat_hospitalization_certificate
//                            )
//                        )
//
//                        actionItemList.add(
//                            3,
//                            ActionItem(
//                                ActionConstants.CUSTOM_ACTION_TYPE_INSPECT,
//                                R.mipmap.ic_chat_inspect,
//                                R.string.input_menu_inspect
//                            )
//                        )
                    }


//                    actionItemList.add(
//                        1,
//                        ActionItem(
//                            ActionConstants.CUSTOM_ACTION_TYPE_USEFUL,
//                            R.mipmap.ic_chat_useful_expressions,
//                            R.string.chat_useful_expressions
//                        )
//                    )


                } else {
                    actionItemList.removeIf {
                        it.action.equals(ActionConstants.CUSTOM_ACTION_TYPE_INSPECT)//检验检查
                    }
                    actionItemList.removeIf {
                        it.action.equals(ActionConstants.CHAT_HOSPITALIZATION_CERTIFICATE)//住院证
                    }
                    actionItemList.removeIf {
                        it.action.equals(ActionConstants.CUSTOM_ACTION_TYPE_USEFUL)//常用语
                    }
                    actionItemList.removeIf {
                        it.action.equals(ActionConstants.CUSTOM_ACTION_TYPE_PRESCRIBING)//处方开药
                    }

                }

                actionItemList.add(
                    ActionItem(
                        ActionConstants.CUSTOM_ACTION_TYPE_REPORT,
                        R.mipmap.ic_chat_report,
                        R.string.input_menu_report
                    )
                )

                actionItemList.removeIf {
                    it.action.equals(ActionConstants.ACTION_TYPE_FILE)//移除文件
                }
                actionItemList.removeIf {
                    it.action.equals(ActionConstants.ACTION_TYPE_LOCATION)//移除地理位置
                }


                return actionItemList
            }

            override fun onCustomInputClick(context: Context, view: View, action: String): Boolean {
                onChatMenuClick?.onMenuCallBack(action)
                return true
            }
        }
        ChatKitClient.setChatUIConfig(chatUIConfig)
    }


    /**
     * 音视频通话初始化
     */
    fun configCallKit() {
        val options: CallKitUIOptions = CallKitUIOptions.Builder() // 必要：音视频通话 sdk appKey，用于通话中使用
            .rtcAppKey(NIM_APP_KEY)
            // 必要：当前用户 AccId
            .currentUserAccId(UserInfoManager.getImUserAccount())
            // 通话接听成功的超时时间单位 毫秒，默认30s
            .timeOutMillisecond(30 * 1000L)
            // 此处为 收到来电时展示的 notification 相关配置，如图标，提示语等。
            .notificationConfigFetcher { invitedInfo: NEInviteInfo ->
                val info = getUserInfoFromLocal(invitedInfo.callerAccId)
                val name = info?.getUserInfoName() ?: invitedInfo.callerAccId
                val callType =
                    if (invitedInfo.callType == ChannelType.AUDIO.value) "视频通话" else "语音通话"
                val content = name + callType
                CallKitNotificationConfig(R.drawable.ic_notification_logo, null, null, content)
            } // 收到被叫时若 app 在后台，在恢复到前台时是否自动唤起被叫页面，默认为 true
            .resumeBGInvitation(true) // 请求 rtc token 服务，若非安全模式则不需设置(V1.8.0版本之前需要配置，V1.8.0及之后版本无需配置)
            //.rtcTokenService((uid, callback) -> requestRtcToken(appKey, uid, callback)) // 自己实现的 token 请求方法
            // 设置初始化 rtc sdk 相关配置，按照所需进行配置
            .rtcSdkOption(NERtcOption()) // 呼叫组件初始化 rtc 范围，NECallInitRtcMode.GLOBAL-全局初始化，
            // NECallInitRtcMode.IN_NEED-每次通话进行初始化以及销毁，全局初始化有助于更快进入首帧页面，
            // 当结合其他组件使用时存在rtc初始化冲突可设置NECallInitRtcMode.IN_NEED
            // 或当结合其他组件使用时存在rtc初始化冲突可设置NECallInitRtcMode.IN_NEED_DELAY_TO_ACCEPT
            .initRtcMode(NECallInitRtcMode.GLOBAL)
            .build()
        // 设置自定义话单消息发送
//        NECallEngine.sharedInstance().setCallRecordProvider(CustomCallOrderProvider())
        // 若重复初始化会销毁之前的初始化实例，重新初始化
        init(CompassAppHelper.getApplication(), options)
        getAuthServiceObserver().observeOnlineStatus(
            Observer { statusCode: StatusCode ->
                if (statusCode == StatusCode.LOGOUT) {
                    destroy()
                }
            } as Observer<StatusCode>,
            true)
    }

    /**
     * 会议初始化
     */
    fun initMeetingKitConfig(neCallback: NECallback<Void>) {
        val config = NEMeetingKitConfig()
        config.appKey = NIM_APP_KEY //应用AppKey
        config.appName = getStringFromResource(R.string.app_name)

        //配置会议时显示前台服务
        val foregroundServiceConfig = NEForegroundServiceConfig()
        foregroundServiceConfig.contentTitle = getStringFromResource(R.string.app_name)
        config.foregroundServiceConfig = foregroundServiceConfig

        NEMeetingKit.getInstance().initialize(CompassAppHelper.getApplication(), config, neCallback)
    }
}